package com.das.mechanic_base.adapter.create;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.StaffListBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundImageView;

import java.util.ArrayList;
import java.util.List;

public class StaffSelectAdapter extends RecyclerView.Adapter<StaffSelectAdapter.StaffSelectHolder> {

    private List<StaffListBean> mList;

    private List<Boolean> sList;

    private Context mContext;

    private int lastIndex = 0;

    private int selfIndex = -1;

    public StaffSelectAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeStaff(List<StaffListBean> mList) {
        this.mList = mList;
        this.sList.clear();
        lastIndex = 0;
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i,mList.get(i).selected);
            if (mList.get(i).selected) {
                selfIndex = i;
                lastIndex = i;
            }
        }
        if (iOnSelectStaff != null) {
            iOnSelectStaff.iOnSelectStaff(mList.get(lastIndex));
        }
        notifyDataSetChanged();
    }

    public void changeStaff(List<StaffListBean> mList,long staffBaseId) {
        this.mList = mList;
        this.sList.clear();
        int staffIndex = 0;
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i,staffBaseId == mList.get(i).id);
            if (staffBaseId == mList.get(i).id) {
                staffIndex = i;
            }
        }
        lastIndex = staffIndex;
        if (iOnSelectStaff != null) {
            iOnSelectStaff.iOnSelectStaff(mList.get(staffIndex));
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StaffSelectHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new StaffSelectHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_staff_select_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull StaffSelectHolder holder, int position) {

//        holder.rv_view.setStrokeColor(sList.get(position) ? ContextCompat.getColor(mContext,R.color.bg_0077FF) : Color.TRANSPARENT);
//        holder.tv_name.setTextColor(sList.get(position) ? ContextCompat.getColor(mContext,R.color.bg_0077FF) : ContextCompat.getColor(mContext,R.color.c333));
        holder.iv_selected.setImageResource(sList.get(position) ? R.mipmap.x3_staff_selected :  R.mipmap.x3_staff_normal);
        if (mContext instanceof Activity){
            try {
                if (!((Activity)mContext).isFinishing()) {
//                    Glide.with(mContext).load(X3StringUtils.getImageUrl(mList.get(position).portrait)).into(holder.rv_view);
                    X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(mList.get(position).portrait),holder.rv_view,R.mipmap.x3_car_owner_header);
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        } else {
            try {
//                Glide.with(mContext).load(X3StringUtils.getImageUrl(mList.get(position).portrait)).into(holder.rv_view);
                X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(mList.get(position).portrait),holder.rv_view,R.mipmap.x3_car_owner_header);

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (0 == position){
            holder.tv_name.setText(mList.get(position).firstName + " " + mList.get(position).lastName + mContext.getString(R.string.set_plan_work_self));
        }else{
            holder.tv_name.setText(mList.get(position).firstName + " " + mList.get(position).lastName);
        }

        if(position == mList.size() -1 ){
            holder.v_bottom.setVisibility(View.INVISIBLE);
        } else {
            holder.v_bottom.setVisibility(View.VISIBLE);
        }
    }

    IOnSelectStaff iOnSelectStaff;

    public StaffSelectAdapter setiOnSelectStaff(IOnSelectStaff iOnSelectStaff) {
        this.iOnSelectStaff = iOnSelectStaff;
        return this;
    }

    public interface IOnSelectStaff {
        void iOnSelectStaff(StaffListBean staffListBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class StaffSelectHolder extends RecyclerView.ViewHolder {
        RoundImageView rv_view;
        TextView tv_name;
        ImageView iv_selected;
        View v_bottom;
        public StaffSelectHolder(@NonNull View itemView) {
            super(itemView);
            rv_view = itemView.findViewById(R.id.rv_view);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_selected = itemView.findViewById(R.id.iv_selected);
            v_bottom = itemView.findViewById(R.id.v_bottom);

            itemView.setOnClickListener(view -> {
                int absoluteAdapterPosition = getAbsoluteAdapterPosition();
                sList.remove(lastIndex);
                sList.add(lastIndex,false);
                sList.remove(absoluteAdapterPosition);
                sList.add(absoluteAdapterPosition,true);
                notifyItemChanged(lastIndex);
                notifyItemChanged(absoluteAdapterPosition);
                lastIndex = absoluteAdapterPosition;
                if (iOnSelectStaff != null) {
                    iOnSelectStaff.iOnSelectStaff(mList.get(absoluteAdapterPosition));
                }
            });
        }
    }
}
